﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_MSDS_Section11
	Inherits System.Web.UI.UserControl
	Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "EVENT FOR CONTROL"

	Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

		Translate()
		If Not IsPostBack Then

			Me.EnableViewState = False
			Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
			If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
				ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
			Else
				Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
			End If
			If Not Request.QueryString("Id").ToString.Equals("0") Then
				hidMSDSID.Value = Request.QueryString("Id")
				btnPrevious.Attributes.Add("onclick", "location.href='Section10.aspx?Id=" & hidMSDSID.Value & "'")
			End If
			CheckData()
		End If
	End Sub
	Protected Sub Translate()
		lbSection11Title.Text = getTranslate("lbSection11Title", Session("LanguageId"), lbSection11Title.Text)
		lbSection11TenTP.Text = getTranslate("lbSection11TenTP", Session("LanguageId"), lbSection11TenTP.Text)
		lbSection11LoaiNguong.Text = getTranslate("lbSection11LoaiNguong", Session("LanguageId"), lbSection11LoaiNguong.Text)
		lbSection11KQ.Text = getTranslate("lbSection11KQ", Session("LanguageId"), lbSection11KQ.Text)
		lbSection11DuongTiepXuc.Text = getTranslate("lbSection11DuongTiepXuc", Session("LanguageId"), lbSection11DuongTiepXuc.Text)
		lbSection11SinhVatThu.Text = getTranslate("lbSection11SinhVatThu", Session("LanguageId"), lbSection11SinhVatThu.Text)
		lblSection11Thanhphan1.Text = getTranslate("lblSection11Thanhphan1", Session("LanguageId"), lblSection11Thanhphan1.Text)
		lblSection11Thanhphan2.Text = getTranslate("lblSection11Thanhphan2", Session("LanguageId"), lblSection11Thanhphan2.Text)
		lblSection11Thanhphan3.Text = getTranslate("lblSection11Thanhphan3", Session("LanguageId"), lblSection11Thanhphan3.Text)
		lbSection11lbCacAnhHuong.Text = getTranslate("lbSection11lbCacAnhHuong", Session("LanguageId"), lbSection11lbCacAnhHuong.Text)
		lbSection11lbAnhHuongDocKhac.Text = getTranslate("lbSection11lbAnhHuongDocKhac", Session("LanguageId"), lbSection11lbAnhHuongDocKhac.Text)
		hidDel.Value = getTranslate("strDelete", Session("LanguageId"), hidDel.Value)
	End Sub
	Protected Sub btnSection01Save_Click(sender As Object, e As EventArgs) Handles btnSave.Click
		Using data As New VSEntities
			Try
				Dim p As MSDSSection11
				p = (From q In data.MSDSSection11 Where q.Section11Id = hidID.Value).FirstOrDefault()
				Dim _isNew = True
				If Not p Is Nothing Then
					_isNew = False
				Else
					p = New MSDSSection11
				End If
				With p
					'.S1110 = txtS1110.Text.Trim
					'.S1111 = txtS1111.Text.Trim
					'.S1112 = txtS1112.Text.Trim
					'.S1113 = txtS1113.Text.Trim
					'.S1114 = txtS1114.Text.Trim

					'.S1120 = txtS1120.Text.Trim
					'.S1121 = txtS1121.Text.Trim
					'.S1122 = txtS1122.Text.Trim
					'.S1123 = txtS1123.Text.Trim
					'.S1124 = txtS1124.Text.Trim

					'.S1130 = txtS1130.Text.Trim
					'.S1131 = txtS1131.Text.Trim
					'.S1132 = txtS1132.Text.Trim
					'.S1133 = txtS1133.Text.Trim
					'.S1134 = txtS1134.Text.Trim

					.S1141 = txtS1141.Text.Trim
					.S1151 = txtS1151.Text.Trim
					.LanguageId = Session("LanguageId")
					.MSDSId = hidMSDSID.Value
				End With
				If _isNew Then
					p.Created = Now
					p.Creator = Session("UserName")
					data.MSDSSection11.AddObject(p)
				Else
					p.Modified = Now
					p.Modifier = Session("UserName")
				End If
				data.SaveChanges()

				'' Xử lý phàn Detail
				hidID.Value = p.Section11Id
				'Delete all by Section03Id
				Dim _sqlString As String = " DELETE FROM SECTION11Detail WHERE Section11ID=" & hidID.Value
				data.ExecuteStoreCommand(_sqlString)
				'' Insert Section03Detail
				If hidValue.Value <> "" Then
					Dim listData = hidValue.Value.Split("$")
					For index = 0 To listData.Count - 1
						Dim _p As New Section11Detail
						If listData(index) <> "" Then
							Dim _lst = listData(index).Split("#")
							If _lst.Length > 4 Then
								_p.Section11Id = hidID.Value
								_p.TenThanhPhan = _lst(0)
								_p.LoaiNguong = _lst(1)
								_p.KetQua = _lst(2)
								_p.DuongTiepXuc = _lst(3)
								_p.SinhVatThu = _lst(4)
								data.Section11Detail.AddObject(_p)
							End If
						End If
					Next
					data.SaveChanges()
				End If

				Insert_App_Log(If(_isNew, "Insert", "Update") & "  MSDS Section11:" & hidMSDSID.Value & "", Function_Name.MSDSOriginal, If(_isNew, Audit_Type.Create, Audit_Type.Edit), Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
				Excute_Javascript("AlertboxRedirect('Lưu dữ liệu thành công.','../../Page/MSDS/Section12.aspx?Id=" & hidMSDSID.Value & "');", Me.Page, True)

			Catch ex As Exception
				log4net.Config.XmlConfigurator.Configure()
				log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
				Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
			Finally
				GC.Collect()
			End Try
		End Using
	End Sub
#End Region
#Region "PRIAVTE SUB AND FUNCTION"
	Sub CheckData()
		Using data As New VSEntities
			Dim LanguageId = Session("LanguageId").ToString()
			Dim p = (From q In data.MSDSSection11 Where q.LanguageId = LanguageId And q.MSDSId = hidMSDSID.Value).FirstOrDefault()
			If Not p Is Nothing Then
				'txtS1110.Text = p.S1110
				'txtS1111.Text = p.S1111
				'txtS1112.Text = p.S1112
				'txtS1113.Text = p.S1113
				'txtS1114.Text = p.S1114

				'txtS1120.Text = p.S1120
				'txtS1121.Text = p.S1121
				'txtS1122.Text = p.S1122
				'txtS1123.Text = p.S1123
				'txtS1124.Text = p.S1124

				'txtS1130.Text = p.S1130
				'txtS1131.Text = p.S1131
				'txtS1132.Text = p.S1132
				'txtS1133.Text = p.S1133
				'txtS1134.Text = p.S1134

				txtS1141.Text = p.S1141
				txtS1151.Text = p.S1151
				hidID.Value = p.Section11Id
			End If
		End Using
	End Sub
#End Region
End Class
